

#if defined(__BORLANDC__)
#pragma comment(lib, "icudt_bcc.lib")
#pragma comment(lib, "icuin_bcc.lib")
#pragma comment(lib, "icuio_bcc.lib")
#pragma comment(lib, "icule_bcc.lib")
#pragma comment(lib, "iculx_bcc.lib")
#pragma comment(lib, "icutu_bcc.lib")
#pragma comment(lib, "icuuc_bcc.lib")
#endif

#if defined(_MSC_VER)
#pragma comment(lib, "icudt.lib")
#pragma comment(lib, "icuin.lib")
#pragma comment(lib, "icuio.lib")
#pragma comment(lib, "icule.lib")
#pragma comment(lib, "iculx.lib")
#pragma comment(lib, "icutu.lib")
#pragma comment(lib, "icuuc.lib")
#endif

#include <stdint.h>
#include "unicode/utypes.h"
#include "unicode/ucsdet.h"
#include "unicode/unistr.h"


/*
int icu_detect(string &s, std::string &t){  
UErrorCode status; UCharsetDetector *csd;  
const UCharsetMatch *cs;
csd = ucsdet_open(&status);
ucsdet_setText(csd, &s[0], s.size(),  &status);
cs= ucsdet_detect(csd,  &status);
int n=100;
if(cs==NULL)  t="BINARY";
else {
n=ucsdet_getConfidence(cs, &status);
if(n<60) t="BINARY";
else t = ucsdet_getName(cs, &status); 
}
ucsdet_close(csd); 
return n; }
*/



int icu_detect(std::string &buffer, std::string &t){  
UErrorCode status = U_ZERO_ERROR;
UCharsetDetector *csd = ucsdet_open(&status);
int matchCount;
ucsdet_setText(csd, &buffer[0], buffer.size(), &status);
const UCharsetMatch **csm = ucsdet_detectAll(csd, &matchCount, &status);
if(csm==NULL)  { t="BINARY"; ucsdet_close(csd);  return 100; }
int n = ucsdet_getConfidence(csm[0], &status);
if(n<35) t="BINARY"; else t = ucsdet_getName(csm[0], &status);
//for(n=0; n<matchCount; n++) cout<<ucsdet_getName(csm[n], &status)<<endl;
ucsdet_close(csd); 
return n; }







/*
string icu_detectALL(string &buffer, std::string &t){  
        UCharsetDetector* csd;
        const UCharsetMatch **csm;
        UErrorCode status = U_ZERO_ERROR;
        csd = ucsdet_open(&status);
int match,matchCount;
        ucsdet_setText(csd, &buffer[0], buffer.size(), &status);
        csm = ucsdet_detectAll(csd, &matchCount, &status);
        for(match = 0; match < matchCount; match += 1) {
            const char *name = ucsdet_getName(csm[match], &status);
            const char *lang = ucsdet_getLanguage(csm[match], &status);
            int32_t confidence = ucsdet_getConfidence(csm[match], &status);
            if (lang == NULL || strlen(lang) == 0)  lang = "**";
            printf("%s (%s) %d\n", name, lang, confidence);       }
        ucsdet_close(csd);   return ""; }
*/




/*
string encode_icu( const std::string &s, std::string from, std::string to) {
icu::UnicodeString src( &s[0], s.size(),  &from[0]);
string t(s.size(), '\0');
int n = src.extract(0, src.length(), &t[0], &to[0]);
t.resize(n);
return t; }
*/



